﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using BLL.Entities.Company;
using BLL.Entities;

namespace HTAProDataImporter.Transport
{
    public class BankAccountTransport : BaseEntityTransport
    {
        public override string CommandText
        {
            get { return "select * from tblBankAccount"; }
        }

        public override void InstanceFromReader(OleDbDataReader DR)
        {
            BankAccount account = BankAccount.CreateNewInstance<BankAccount>();

            account.CompanyID = DataStore.ActiveCompanyID;

            account.Title = DR["AccountName"].ToString();
            account.BankName = DR["BankName"].ToString();
            account.AccountHolder = DR["AccountHolder"].ToString();
            account.AccountNumber = DR["AccountNumber"].ToString();

            try
            {
                account.AccountType = DataStore.BankAccountTypeValues[DR["fkBankTypeID"].ToString()];
            }
            catch(Exception ex)
            {
                Warnings.Add(string.Format("Unknown Account Type {0}", DR["fkBankTypeID"]));
            }

            account.Branch = DR["Branch"].ToString();
            account.BranchAddress.AddressLine = DR["Address"].ToString();
            account.BranchAddress.City = DR["City"].ToString();
            account.BranchAddress.Province = DR["Province"].ToString();
            account.BranchAddress.PostalCode = DR["PostalCode"].ToString();

            account.Contact.PhoneWork = DR["PhoneNumber"].ToString();
            account.Contact.Fax = DR["FaxNumber"].ToString();

            int chq = 0;
            Int32.TryParse(DR["StartingChequeNumber"].ToString(), out chq);
            account.StartingChequeNumber = chq;

            chq = 0;
            Int32.TryParse(DR["CurrentChequeNumber"].ToString(), out chq);
            account.StartingChequeNumber = chq;


            Decimal balance = 0.0M;
            Decimal.TryParse(DR["StartingBalance"].ToString().Trim('$'), out balance);
            account.StartingBalance = balance;

            balance = 0.0M;
            Decimal.TryParse(DR["CurrentBalance"].ToString().Trim('$'), out balance);
            account.CurrentBalance = balance;


            DataStore.BankAccounts.Add(DR["pkBankAccountID"].ToString(), account);


        }
    }
}
